<?php

class System_Array
{

    public static function getByKey($arr, $key, $default = null)
    {
        return isset($arr[$key]) ? $arr[$key] : $default;
    }

    public static function getColumn($arr, $name)
    {
        if(empty($arr)) {
            return array();
        }
        $newArray = array();
        foreach($arr as $a) {
            $newArray[] = isset($a[$name]) ? $a[$name] : null;
        }
        return array_filter(array_unique($newArray));
    }

    /*
        $arr1 = array(
            array('user_id' => 1, 'deal_id' => 2, 'name' => 'deal name2'),
            array('user_id' => 2, 'deal_id' => 3, 'name' => 'deal name3'),
        );
        $arr2 = array(
            array('user_id' => 1, 'username' => 'sunseesiu'),
            array('user_id' => 2, 'username' => 'ambitear'),
        );
    */
    public static function multiMerge($arrA, $arrB, $keyA, $keyB = null, $params = array())
    {
        if(empty($arrA) || !is_array($arrA)) {
            return array();
        }
        if(empty($arrB) || !is_array($arrB)) {
            return $arrA;
        }

        if(empty($keyB)) {
            $keyB = $keyA;
        }

        $dataMap = array();
        foreach($arrB as $arr) {
            if(!isset($arr[$keyB])) {
                throw new Exception('Undefined second merge key: ' . $keyB);
            }
            // Select arrB fields
            if($params) {
                $newArr = array();
                foreach($params as $name => $param) {
                    $newArr[$param] = isset($arr[$name]) ? $arr[$name] : null;
                }
            } else {
                $newArr = $arr;
            }
            $dataMap[$arr[$keyB]] = $newArr;
        }

        $defaultValue = array();
        if(is_array($newArr)) {
            foreach($newArr as $key => $value) {
                $defaultValue[$key] = null;
            }
        }

        foreach($arrA as $k => $v) {
            if(!isset($v[$keyA])) {
                throw new Exception('Undefined first merge key: ' . $keyA);
            }
            $m = isset($dataMap[$v[$keyA]]) ? $dataMap[$v[$keyA]] : $defaultValue;
            $arrA[$k] = array_merge($v, $m);
        }
        return $arrA;
    }

    public static function recursiveMerge($array1, $array2)
    {
        reset ($array2);
        while(key($array2)!==null) {
            if (is_array(current($array2)) && isset($array1[key($array2)])) {
                $array1[key($array2)] = self::recursiveMerge($array1[key($array2)],current($array2));
            } else {
                $array1[key($array2)] = current($array2);
            }
            next($array2);
        }
        return $array1;
    }

    public static function mergeByKey($arrayMain, $arrayB, $key)
    {
        if(empty($arrayA)) {
            return array();
        }

        if(empty($arrayA) || !is_array($arrayB)) {
            return $arrayB;
        }

        if(empty($arrayA) || !is_array($arrayB)) {
            return $arrayA;
        }

        $count = count($arrayA);
        for($i = 0; $i <= $count; $i++) {
            $arrayA[$i] = array_merge(array_diff($arrayA[$i], $arrayB[$i]), array_diff($arrayB[$i], $arrayA[$i]));
        }
        return $arrayA;
    }

    public function mergeIn($main, $sub, $refKey, $newKey)
    {
        if(empty($main)) {
            return array();
        }

        if(empty($sub)) {
            return $main;
        }

        $newSub = array();
        foreach($sub as $subValue) {
            if(!isset($subValue[$refKey])) {
                throw new Exception('Undefined sub merge array key ' . $refKey);
            }
            $newSub[$subValue[$refKey]] = $subValue;
        }

        foreach($main as $key => $value) {
            if(!isset($value[$refKey])) {
                throw new Exception('Undefined main merge array key ' . $refKey);
            }
            $mainItem = $value[$refKey];
            $main[$key][$newKey] = isset($newSub[$mainItem]) ? $newSub[$mainItem] : null;
        }
        return $main;
    }

}
